using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VLRSUBST
    {
        public DataRow Convert2LRSUBST(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryLRSUBST(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["LRSMANDANT"] = " ".PadRight(2 ); 
         dwReturn["LRSREZART"] = " ".PadRight(1 ); 
         dwReturn["LRSREZNR"] = " ".PadRight(10 ); 
         dwReturn["LRSREZST"] = " ".PadRight(1 ); 
         dwReturn["LRSPOS"] = " ".PadRight(1 ); 
         dwReturn["LRSSUB"] = " ".PadRight(8 ); 
         dwReturn["LRSFAKT"] = " ".PadRight(0 ); 
         dwReturn["LRSSORT"] = " ".PadRight(5 ); 
         dwReturn["LRSPROZFKT"] = " ".PadRight(6 ); 
         dwReturn["LRSVLCODE"] = " ".PadRight(15 ); 
         dwReturn["LRSMUKL"] = " ".PadRight(6 ); 
         dwReturn["LRSFPAR1"] = " ".PadRight(0 ); 
         dwReturn["LRSFPAR2"] = " ".PadRight(0 ); 
         dwReturn["LRSFPAR3"] = " ".PadRight(0 ); 
         dwReturn["LRSFPAR4"] = " ".PadRight(0 ); 
         dwReturn["LRSFPAR5"] = " ".PadRight(0 ); 
         dwReturn["LRSBEM"] = " ".PadRight(10 ); 
         dwReturn["LRSFILLER"] = " ".PadRight(3 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapLRSUBST(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveLRSUBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryLRSUBST(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapLRSUBST(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertLRSUBST(dwLocal, ITXDBVERSION);
            }
            UpdateLRSUBST(dwLocal);
        }
        public void InsertLRSUBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO LRSUBST(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryLRSUBST(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM LRSUBST WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM LRSUBST  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "LRSUBST";
            Return = Result;
            return Return;
        }
        public void DeleteLRSUBST(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM LRSUBST   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateLRSUBST(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE LRSUBST SET "
                    +"LRSMANDANT= :LRSMANDANT,"
                    +"LRSREZART= :LRSREZART,"
                    +"LRSREZNR= :LRSREZNR,"
                    +"LRSREZST= :LRSREZST,"
                    +"LRSPOS= :LRSPOS,"
                    +"LRSSUB= :LRSSUB,"
                    +"LRSFAKT= :LRSFAKT,"
                    +"LRSSORT= :LRSSORT,"
                    +"LRSPROZFKT= :LRSPROZFKT,"
                    +"LRSVLCODE= :LRSVLCODE,"
                    +"LRSMUKL= :LRSMUKL,"
                    +"LRSFPAR1= :LRSFPAR1,"
                    +"LRSFPAR2= :LRSFPAR2,"
                    +"LRSFPAR3= :LRSFPAR3,"
                    +"LRSFPAR4= :LRSFPAR4,"
                    +"LRSFPAR5= :LRSFPAR5,"
                    +"LRSBEM= :LRSBEM,"
                    +"LRSFILLER= :LRSFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("LRSMANDANT", OracleType.NVarChar).Value = dwData["LRSMANDANT"].ToString(); 
          or1.Parameters.Add("LRSREZART", OracleType.NVarChar).Value = dwData["LRSREZART"].ToString(); 
          or1.Parameters.Add("LRSREZNR", OracleType.NVarChar).Value = dwData["LRSREZNR"].ToString(); 
          or1.Parameters.Add("LRSREZST", OracleType.NVarChar).Value = dwData["LRSREZST"].ToString(); 
          or1.Parameters.Add("LRSPOS", OracleType.NVarChar).Value = dwData["LRSPOS"].ToString(); 
          or1.Parameters.Add("LRSSUB", OracleType.NVarChar).Value = dwData["LRSSUB"].ToString(); 
          or1.Parameters.Add("LRSFAKT", OracleType.NVarChar).Value = dwData["LRSFAKT"].ToString(); 
          or1.Parameters.Add("LRSSORT", OracleType.NVarChar).Value = dwData["LRSSORT"].ToString(); 
          or1.Parameters.Add("LRSPROZFKT", OracleType.NVarChar).Value = dwData["LRSPROZFKT"].ToString(); 
          or1.Parameters.Add("LRSVLCODE", OracleType.NVarChar).Value = dwData["LRSVLCODE"].ToString(); 
          or1.Parameters.Add("LRSMUKL", OracleType.NVarChar).Value = dwData["LRSMUKL"].ToString(); 
          or1.Parameters.Add("LRSFPAR1", OracleType.NVarChar).Value = dwData["LRSFPAR1"].ToString(); 
          or1.Parameters.Add("LRSFPAR2", OracleType.NVarChar).Value = dwData["LRSFPAR2"].ToString(); 
          or1.Parameters.Add("LRSFPAR3", OracleType.NVarChar).Value = dwData["LRSFPAR3"].ToString(); 
          or1.Parameters.Add("LRSFPAR4", OracleType.NVarChar).Value = dwData["LRSFPAR4"].ToString(); 
          or1.Parameters.Add("LRSFPAR5", OracleType.NVarChar).Value = dwData["LRSFPAR5"].ToString(); 
          or1.Parameters.Add("LRSBEM", OracleType.NVarChar).Value = dwData["LRSBEM"].ToString(); 
          or1.Parameters.Add("LRSFILLER", OracleType.NVarChar).Value = dwData["LRSFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
